#!/bin/bash

# JayVii
# https://notabug.org/jayvii
# update-NodeJS v0.1

# setting variables
NODE_VER=$1
BUILD_DIR=$2
CLEAN=$3
STEP=0

# show help, if no options are set
if [ -z "$NODE_VER" ]; then
	echo "########## [UPDATE-NODEJS HELP]"
	echo "#"
	echo "# ./update-nodejs.run [VERSION] [BUILD-DIR] [CLEAN]"
	echo "#"
	echo "##### [VERSION]"
	echo "# specifies, which NodeJS-Version to install"
	echo "# ex: './update-nodejs.run 5.5.0'"
	echo "# Find all releases at https://github.com/nodejs/node/archive/"
	echo "#"
	echo "##### [BUILD-DIR]"
	echo "# specifies the PARENT build-directory to use"
	echo "# ex: './update-nodejs.run 5.5.0 ~/my_packages/node' will build in '~/my_packages/node/node-5.5.0'"
	echo "# Default: ~/build/nodejs"
	echo "#"
	echo "##### [CLEAN]"
	echo "# determines, wether to remove the build-dir after install"
	echo "# ex: './update-nodejs.run 5.5.0 YES'"
	echo "# 'YES' will remove it, anything else means 'NO'"
	echo "# Default: NO"
	echo "#"
	echo "##### Dependencies:"
	echo "# https://github.com/nodejs/node#build"
	echo "# gcc  	[>= 4.8]"
	echo "# g++  	[>= 4.8]"
	echo "# clang  	[>= 3.8]"
	echo "# clang++ [>= 3.8]"
	echo "# python 	[=  2.7]"
	echo "# GNU make [>= 3.8.1]"
	echo "# tar & wget / git"
	exit 1
fi

# control output
echo ""
echo "##############################################################"
echo "Start building process in 10 seconds."
echo "Version:   ${NODE_VER}"
echo "Directory: ${BUILD_DIR}"
echo "Cleaning:  ${CLEAN}"
echo ""
echo "Is this correct? (CTRL + C) to interrupt."
echo "##############################################################"
echo ""
sleep 10

# setting fallback build-dir
if [ -z "$BUILD_DIR" ]; then
	STEP=$((${STEP} + 1))
	echo "[${STEP}] No build directory specified. Using ~/build/nodejs"
	sleep 2
	BUILD_DIR="~/build/nodejs"
fi

# navigate into build-dir parent
mkdir -p "${BUILD_DIR}"
cd "${BUILD_DIR}"

# pull from github if node-git already exists
if [ "$NODE_VER" = git ] && [ -d "node-git" ]; then
	STEP=$((${STEP} + 1))
	echo ""
	echo "[${STEP}] Updating ${BUILD_DIR}/node-${NODE_VER} via 'git pull'"
	sleep 2
	cd node-${NODE_VER}
	git pull
	cd ..
fi

# clone from github if node-git does not exist
if [ "$NODE_VER" = git ] && ! [ -d "node-git" ]; then
	STEP=$((${STEP} + 1))
	echo ""
	echo "[${STEP}] Downloading from https://github.com/nodejs/node via 'git clone'"
	sleep 2
	git clone https://github.com/nodejs/node node-${NODE_VER}
fi

# use node-version if it exists
if [ "$NODE_VER" != git ] && [ -d "node-${NODE_VER}" ]; then
	STEP=$((${STEP} + 1))
	echo ""
	echo "[${STEP}] ${BUILD_DIR}/node-${NODE_VER} already exists."
	echo "[${STEP}] Not downloading anything."
	sleep 2
fi

# download release if it does not already exist
if [ "$NODE_VER" != git ] && ! [ -d "node-${NODE_VER}" ]; then
	STEP=$((${STEP} + 1))
	echo ""
	echo "[${STEP}] Downloading https://github.com/nodejs/node/archive/v${NODE_VER}.tar.gz"
	sleep 2
	wget https://github.com/nodejs/node/archive/v${NODE_VER}.tar.gz
	tar -zxvf v${NODE_VER}.tar.gz
	rm v${NODE_VER}.tar.gz
fi

# build process
STEP=$((${STEP} + 1))
echo ""
echo "[${STEP}] #############################"
echo "[${STEP}] Ready to build NodeJS ${NODE_VER}"
echo ""
sleep 2
cd node-${NODE_VER}
./configure
make

# install process
STEP=$((${STEP} + 1))
echo ""
echo "[${STEP}] ##############################"
echo "[${STEP}] Ready to install NodeJS ${NODE_VER}"
echo "[${STEP}] Please enter your root-password."
echo ""
su -c "make install" root

# removing build-dir
if [ "$CLEAN" = YES ]; then
	STEP=$((${STEP} + 1))
	echo ""
	echo "[${STEP}] Removing build-dir."
	echo ""
	rm -rf "${BUILD_DIR}/node-${NODE_VER}"
fi

# linking node to nodejs
STEP=$((${STEP} + 1))
echo ""
echo "[${STEP}] ##############################"
echo "[${STEP}] Linking '/usr/local/bin/node' to '/usr/bin/node'."
echo "[${STEP}] Please enter your root-password."
echo "[${STEP}] Or enter wrong password to skip."
echo ""
if [ -f /usr/bin/nodejs ]; then
	su -c "mv /usr/bin/nodejs /usr/bin/nodejs.real
		ln -s /usr/local/bin/node /usr/bin/nodejs" root
else
	su -c "ln -s /usr/local/bin/node /usr/bin/nodejs" root
fi

# checking version of installed nodejs
STEP=$((${STEP} + 1))
echo ""
echo "[${STEP}] ##############################"	
echo "[${STEP}] DONE!"
echo "[${STEP}] installed NodeJS-Version: $(node -v)"
echo "[${STEP}] installed NPM-Version: $(npm -v)"
